import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution49 {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String,List<String>> map=new HashMap<>();
        for (String str : strs) {
            int[] f=new int[26];
            for (char c : str.toCharArray()) {
                f[c-'a']++;
            }
            StringBuilder stringBuilder=new StringBuilder();
            for (int i = 0; i < 26; i++) {
                if (f[i]!=0){
                    stringBuilder.append((char)(i+'a')).append(f[i]);
                }
            }
            List<String> list=map.getOrDefault(stringBuilder.toString(),new ArrayList<>());
            list.add(str);
            map.put(stringBuilder.toString(),list);
        }
        List<List<String>> ans=new ArrayList<>(map.values());
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution49().groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"}));
    }
}
